package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityShopcar;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanShopCar;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.fragment.FragmentShopcar;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;

/**
 * Created by nero on 17-6-23.
 */

public class AdapterShopCarRecylerView_activity extends RecyclerView.Adapter<AdapterShopCarRecylerView_activity.Holder> {

    Context context;
    ArrayList<BeanShopCar.goods> arrayList;

    ActivityShopcar fragmentShopcar;

    public AdapterShopCarRecylerView_activity(Context context, ArrayList<BeanShopCar.goods> arrayList, ActivityShopcar fragmentShopcar) {
        this.context = context;
        this.arrayList = arrayList;
        this.fragmentShopcar = fragmentShopcar;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate( R.layout.adapter_shopcar, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final BeanShopCar.goods godBean = arrayList.get(position);

        holder.tv_money.setText("￥"+TextUtil.getText(godBean.getShop_price(), "0") );
//
//        if (godBean.isChoose()) {
//            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
//        } else {
//            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.textColorGray_low));
//        }
//
//        //点击选中
//        holder.icon_choose.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                godBean.setChoose(!godBean.isChoose());
//                AdapterShopCarRecylerView_activity.this.notifyDataSetChanged();
//                fragmentShopcar.setMoney();
//            }
//        });
//        //点击选中
//        holder.ll_root.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                godBean.setChoose(!godBean.isChoose());
//                AdapterShopCarRecylerView_activity.this.notifyDataSetChanged();
//                fragmentShopcar.setMoney();
//            }
//        });

        holder.tv_num.setText(godBean.getNumber() + "");
        holder.tv_xnum.setText("x " + godBean.getNumber());
        //减少数量
        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //减少
                try {
                    int num = godBean.getNumber();
                    if (num > 1) {
                        num--;
                        godBean.setNumber(num);
                    }

                    holder.tv_num.setText(num + "");
                    holder.tv_xnum.setText("x " + num);

                    fragmentShopcar.setMoney();
                } catch (Exception e) {
                }
            }
        });
        //增加数量
        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //减少
                try {
                    int num = godBean.getNumber();
                    if (num < Integer.parseInt(godBean.getStore_count())) {
                        num++;
                        godBean.setNumber(num);


                        holder.tv_num.setText(num + "");
                        holder.tv_xnum.setText("x " + num);
                        fragmentShopcar.setMoney();
                    }

                } catch (Exception e) {

                }
            }
        });
        holder.tv_name.setText(TextUtil.getText(godBean.getGoods_name(),"暂无名称"));
        holder.tv_shuxing.setText(TextUtil.getText(godBean.getSpec_name(),"暂无简介"));
        ImageUtils.BindImage(context,holder.iv_image, Constants.IMAGE_HOST+godBean.getOriginal_img(),1);
    }


    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        IconView icon_choose;
        ImageView iv_image;
        TextView tv_name;
        TextView tv_shuxing;
        TextView tv_money;
        TextView tv_cut;
        TextView tv_num;
        TextView tv_add;
        TextView tv_del;
        TextView tv_xnum;
        RelativeLayout ll_root;

        public Holder(View itemView) {
            super(itemView);
            icon_choose = itemView.findViewById(R.id.icon_choose);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_shuxing = itemView.findViewById(R.id.tv_shuxing);
            tv_money = itemView.findViewById(R.id.tv_money);
            tv_cut = itemView.findViewById(R.id.tv_cut);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_add = itemView.findViewById(R.id.tv_add);
            tv_del = itemView.findViewById(R.id.tv_del);
            ll_root = itemView.findViewById(R.id.ll_root);
            tv_xnum = itemView.findViewById(R.id.tv_xnum);
        }
    }
}
